package cn.org.donald.service.impl;

import cn.org.donald.framework.handler.ServiceException;
import cn.org.donald.framework.pojo.dto.PageDto;
import cn.org.donald.framework.pojo.dto.RequestInfo;
import cn.org.donald.framework.util.BeanUtil;
import cn.org.donald.framework.util.ListUtil;
import cn.org.donald.mapper.GameMapper;
import cn.org.donald.pojo.dto.game.TypeDto;
import cn.org.donald.pojo.entity.game.Game;
import cn.org.donald.pojo.entity.game.Type;
import cn.org.donald.mapper.TypeMapper;
import cn.org.donald.service.game.TypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Donald
 * @since 2020-12-12
 */
@Service(interfaceClass = TypeService.class, version = "1.0.0", group = "donald")
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private GameMapper gameMapper;

    @Autowired
    private RequestInfo requestInfo;

    /**
     * 新增游戏类型
     *
     * @param typeDto
     * @return
     */
    @Override
    public boolean add(TypeDto typeDto) {
        if (typeDto.getId()!=null){
            typeDto.setId(null);
        }
        Type type = new Type();
        BeanUtil.copyProperties(typeDto,type);
        return super.save(type);
    }

    /**
     * 分页查询游戏类型列表
     *
     * @param pageDto
     * @return
     */
    @Override
    public IPage<Type> findPage(PageDto<Type> pageDto) {
        System.out.println("userId:"+ requestInfo.getUserId());
        System.out.println("authorization:"+ requestInfo.getAuthorization());

        QueryWrapper<Type> queryWrapper = null;
        Type query = pageDto.getQuery();
        if (!BeanUtil.isEmptyOrClassFieldAllEmpty(query)){
            queryWrapper = new QueryWrapper<>(query);
        }
        return typeMapper.selectPage(pageDto, queryWrapper);
    }

    /**
     * @param typeDto
     * @return
     */
    @Override
    public boolean editById(TypeDto typeDto) {
        if (typeDto.getId()==null){
            return false;
        }
        Type type = new Type();
        BeanUtil.copyProperties(typeDto,type);
        return super.updateById(type);
    }

    /**
     * 根据id查询游戏类型详情
     *
     * @param id
     * @return
     */
    @Override
    public Type findOneById(Long id) {
        return super.getById(id);
    }

    /**
     * 根据id删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional( rollbackFor = Exception.class )
    public boolean deleteById(List<Long> ids) {
        // 删除类型前确保没有绑定的游戏
        QueryWrapper<Game> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("type_id", ids);
        List<Game> gameList = gameMapper.selectList(queryWrapper);
        if (!ListUtil.isEmptyOrHasClassFieldAllEmpty(gameList)){
            throw new ServiceException("该类型下还存在游戏记录,请先对该游戏进行删除或修改其类型");
        }
        return typeMapper.deleteBatchIds(ids) > 0;
    }
}
